﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Medianamik.Core
{
    [Serializable]
    public class TypeTemplate : IEquatable<TypeTemplate>
    {

        #region Fields


        #endregion

        #region .ctors

 

        #endregion

        #region Events



        #endregion

        #region Properties

        public Guid WebSiteId { get {
            return _webSiteId;
        }
            set
            {
                _webSiteId = value;
                _webSite = null;
            }
        }
        Guid _webSiteId;

        
        public WebSite WebSite
        {
            get
            {
                if (_webSite == null)
                {
                    _webSite = GetAssociatedWebsite();
                }
                return _webSite;
            }
        }

        protected virtual WebSite GetAssociatedWebsite()
        {
            return NodeManager.GetNode<WebSite>(WebSiteId);
        }

        [NonSerialized]
        WebSite _webSite;
        

        public Guid TemplateId
        {
            get
            {
                return _templateId;
            }
            set
            {
                _templateId = value;
                _template = null;
            }
        }
        Guid _templateId;

        
        public PageTemplate Template
        {
            get
            {
                if (_template == null)
                {
                    _template = GetAssociatedPageTempate();
                }
                return _template;
            }
        }

        protected virtual PageTemplate GetAssociatedPageTempate()
        {
            return NodeManager.GetNode<PageTemplate>(TemplateId);
        }

        [NonSerialized]
        PageTemplate _template;


        public Guid? NodeTypeId
        {
            get
            {
                return _nodeTypeId;
            }
            set
            {
                _nodeTypeId = value;
                _nodeType = null;
            }
        }
        Guid? _nodeTypeId;

        
        public NodeType NodeType
        {
            get
            {
                if (_nodeType == null && NodeTypeId.HasValue)
                {
                    _nodeType = NodeTypeManager.GetNodeType(NodeTypeId.Value);
                }
                return _nodeType;
            }
        }
        [NonSerialized]
        NodeType _nodeType;

        #endregion

        #region Methods


        #endregion

        #region IEquatable<TypeTemplate> Members

        public bool Equals(TypeTemplate other)
        {
            if (Object.ReferenceEquals(other, null))
            {
                return false;
            }
            if (Object.ReferenceEquals(other, this))
            {
                return true;
            }
            return (this._webSiteId == other._webSiteId) &&
                (this._templateId == other._templateId) &&
                (this._nodeTypeId == other._nodeTypeId);
        }

        public override int GetHashCode()
        {
            return _webSiteId.GetHashCode() ^ _templateId.GetHashCode()
                ^ _nodeTypeId.GetHashCode();
        }

        #endregion
    }
}
